<?php

public class DeliveryPromises
{
	private $cache;
	private $oms;
	private $rule_model;
	private $location_model;

	function __construct($cache,$oms, $location_model, $rule_model)
	{
		
	}
	
	
	
	/*
	* 	Get delivery promise for a given sku and location
	*/
	private function _get($sku,$location,$stock,$rules)
	{
		//
		// get result from cache 
		//
		$result = $cache->get('');
		if ($result)
			return $result;
		
		$t = 0;
		$set_t = null;
		foreach ($rules as $rule)
		{
			$r = $rule->evaluate($sku,$location,$stock,now());
			if ($r['fired']==RULE_FIRED)
			{
				if ($r['result_type']==RULE_RESULT_TYPE_AMOUNT_OF_TIME)
					$t+=$r['result'];
				else if ($r['result_type']==RULE_RESULT_TYPE_SET_TIME)
				{
					return $r;
					break;
				}
			}
		}
		
		$result =  array('result_type'=>RULE_RESULT_TYPE_AMOUNT_OF_TIME,'result'=>$t);
		$cache->set('',$result);
		return $result;
	}
	
	public function get($sku,$location)
	{
		$stock = $this->oms->get_stock($sku);
		$rules = $this->rule_model->getAll();
		return _get($sku,$location,$stock,$rules);
	
	}
	
	public function getAllLocations($sku)
	{
		$stock = $this->oms->get_stock($sku);
		$rules = $this->rule_model->getAll();	
		$locations = $location_model->getAll();
		
		$result=array();
		foreach ($locations as $l)
		{
			$r = _get($sku,$l);
			$r['location'=>$l];	
			$result[]=$r;
		}
	}
	
}
	
public class Rule
{
	private $id;
	private $order; 
	private $comment; 
	private $active_from; 
	private $active_to; 
	private $conditions;
	private $action;
	
	function __construct($id, $order, $comment,$active_from,$active_to,$conditions,$action) 
	{
	}
	
	/*
	*	
	*/
	private function _parseConditions()
	{
		return json_decode($this->conditions);
	}
	
	/*
	*	
	*/
	private function _parseAction()
	{
		return json_decode($this->action);
	}	
	
	//
	// Check if condition is TRUE
	//
	private function _checkCondition($sku,$location,$stock,$date)
	{
		$conditions = _getConditions();
		foreach ($conditions as $cd)
		{
			switch ($c->type)
			{
				case RULE_CONDITION_LOCATION:
				case RULE_CONDITION_SKU:
				case RULE_CONDITION_DATE:
				case RULE_CONDITION_STOCK:
				default: 
			}
		}
	}
	
	public function evaluate($sku,$location,$stock,$date)
	{
		$fired = _checkCondition();
		if ($fired==0)
			return array('fired'=>0,'result_type'=>null,'result'=>null);
		
		$action = _parseAction();
		switch ($action->type)
		{
			case RULE_ACTION_GET_LOCATION_TIME:
			case RULE_ACTION_GET_SUPLIER_LEAD_TIME:
			case RULE_ACTION_SET_TIME:
			case RULE_ACTION_SET_TARGET_TIME:
			default: 			
		}
		
		return array('fired'=>1,'result_type'=>null,'result'=>null);
	}
}

public class LocationDataModel
{
	private $db;
	private $cache;
	
	
	public function getAll()
	{

	}
	
	function __construct($db, $cache)
	{
		
	}	
}

public class RuleModel
{
	private $db;
	private $cache;
	
	//
	// Retrieve all rules 
	//
	public function getAll()
	{
		
	}
	
	function __construct($db, $cache)
	{
		
	}
}